package com.hpjz.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Data;
import lombok.extern.log4j.Log4j;

@Log4j
@Data
public class ParameterBean implements Serializable {
	private static final long serialVersionUID = 1L;
	/** 接口编号 */
	private Integer id;
	/** 接口平台 */
	private String platform;
	/** 调用版本 */
	private String version;
	/** 接口参数 */
	private Object parameter;
	/** 用户id */
	private String uid;

	/***
	 * 默认构造
	 * 
	 * @return
	 */
	public Object getParameter() {
		if (parameter instanceof ArrayList)
			this.parameter = new HashMap<String, Object>();
		if (parameter == null)
			this.parameter = new HashMap<String, Object>();
		return parameter;
	}

	/**
	 * 转换指定bean
	 * 
	 * @param cls
	 * @return
	 */
	public Object getParameter(Class<?> cls) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(parameter);
			parameter = mapper.readValue(json, cls);
		} catch (Exception e) {
			log.error("参数转换出错:", e);
			e.printStackTrace();
		}
		return parameter;
	}

	/**
	 * string转json
	 * 
	 * @param cls
	 *            要转换的类型
	 * @param str
	 *            待转换的字符串
	 * @return Object
	 */
	public Object getParameter(Class<?> cls, String str) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			parameter = mapper.readValue(str, cls);
		} catch (Exception e) {
			log.error("参数转换出错:", e);
			e.printStackTrace();
		}
		return parameter;
	}

	/**
	 * 类转换string
	 */
	public String toString() {
		String str = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			str = mapper.writeValueAsString(this);
		} catch (JsonProcessingException e) {
			log.info("类转换json出错:", e);
			e.printStackTrace();
		} catch (Exception e) {
			log.info("类转换json出错:", e);
			e.printStackTrace();
		}
		return str;
	}
}
